package com.example.administrator.day35_encryptiondemo1608;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class MainActivity extends AppCompatActivity {

    private String path="http://api.m.mtime.cn/Showtime/LocationMovies.api?locationId=";

    private String url1="https://www.baidu.com/baidu?tn=monline_3_dg&ie=utf-8&wd=%E6%AF%9B%E6%B3%BD%E4%B8%9C";

    private String biluochun="%E7%A2%A7%E8%9E%BA%E6%98%A5";

    private String tea="http://sns.maimaicha.com/api?apikey=b4f4ee31a8b9acc866ef2afb754c33e6&format=json&method=news.searcListByTitle&search=%E7%A2%A7%E8%9E%BA%E6%98%A5";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //URLEncode编码
//        try {
//            String encode = URLEncoder.encode("%", "UTF-8");
//            path=path+encode+"s";
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
        //编码
        String s = URLEncoder.encode("毛泽东");
        Log.d("1608", "onCreate: "+s);

        //解码
        String str="%E6%AF%9B%E6%B3%BD%E4%B8%9C";
        Log.d("1608", "onCreate: "+ URLDecoder.decode(str));

        initData();
    }

    private void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                InputStream is=null;
                ByteArrayOutputStream baos=null;
                try {
                    URL url=new URL(url1);
                    HttpURLConnection conn= (HttpURLConnection) url.openConnection();
                    conn.setConnectTimeout(3*1000);
                    if (conn.getResponseCode()==200){
                        is=conn.getInputStream();
                        baos=new ByteArrayOutputStream();

                        byte[] b=new byte[1024*8];
                        int len=0;
                        while ((len=is.read(b))!=-1){
                            baos.write(b,0,len);
                        }
                        byte[] bytes = baos.toByteArray();
                        Log.d("1608", "run: "+new String(bytes));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }finally {
                    if (baos != null) {
                        try {
                            baos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (is != null) {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        });
    }
}
